package com.study.controller;

import com.study.common.DateUtil;
import com.study.common.ErrorCode;
import com.study.entity.BlogUser;
import com.study.entity.TFriendMessage;
import com.study.service.ContentFeignService;
import com.study.service.UserService;
import com.vedeng.study.model.dto.BlogDTO;
import com.vedeng.study.request.BlogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ContentFeignService contentFeignService;

    @Value("${is.sign}")
    private String isSign;


    @RequestMapping("/toSign")
    public ModelAndView toSign(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/user/sign");
        return modelAndView;
    }

    @RequestMapping("/toAddFriend")
    public ModelAndView toAddFriend(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/user/addfriend");
        return modelAndView;
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/sign")
    public ModelAndView userSign(BlogUser user){
        ModelAndView modelAndView = new ModelAndView();
        //先判断是否还允许注册
        if (!isSign.equals("1")){
            //不为1，不允许注册
            modelAndView.addObject("errorMessage",ErrorCode.IS_NOT_ALLOW_SIGN.getMessage());
            modelAndView.setViewName("/page/user/error");
            return modelAndView;
        }
        if (StringUtils.isEmpty(user.getName())){
            modelAndView.addObject("errorMessage",ErrorCode.NAME_NOT_BE_NULL.getMessage());
            modelAndView.setViewName("/page/user/error");
            return modelAndView;
        }
        if (StringUtils.isEmpty(user.getMobile())){
            modelAndView.addObject("errorMessage",ErrorCode.MOBILE_NOT_BE_NULL.getMessage());
            modelAndView.setViewName("/page/user/error");
            return modelAndView;
        }
        if (StringUtils.isEmpty(user.getUsername())){
            modelAndView.addObject("errorMessage",ErrorCode.USERNAME_NOT_BE_NULL.getMessage());
            modelAndView.setViewName("/page/user/error");
            return modelAndView;
        }
        if (StringUtils.isEmpty(user.getPassword())){
            modelAndView.addObject("errorMessage",ErrorCode.PASSWORD_NOT_BE_NULL.getMessage());
            modelAndView.setViewName("/page/user/error");
            return modelAndView;
        }
        //手机号查询
        List<BlogUser> blogUsersByMobile = userService.getUserByMobile(user.getMobile());
        if (!CollectionUtils.isEmpty(blogUsersByMobile)){
            modelAndView.addObject("errorMessage",ErrorCode.MOBILE_IS_EXIT.getMessage());
            modelAndView.setViewName("/page/user/error");
            return modelAndView;
        }
        //查询用户名
        List<BlogUser> blogUsers = userService.getUserByUsername(user.getUsername());
        if (!CollectionUtils.isEmpty(blogUsers)){
            modelAndView.addObject("errorMessage",ErrorCode.USERNAME_IS_EXIT.getMessage());
            modelAndView.setViewName("/page/user/error");
            return modelAndView;
        }
        Integer i = userService.userSign(user);
        if (i==1){
            modelAndView.setViewName("page/user/login");
            return modelAndView;
        }else {
            modelAndView.addObject("errorMessage",ErrorCode.SIGN_FAIL.getMessage());
            modelAndView.setViewName("/page/user/error");
            return modelAndView;
        }
    }


    /**
     * 用户登录
     * @param blogUser
     * @return
     */
    @PostMapping("/login")
    public ModelAndView login(BlogUser blogUser, HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        if (StringUtils.isEmpty(blogUser.getUsername())){
            modelAndView.addObject("errorMessage",ErrorCode.USERNAME_NOT_BE_NULL.getMessage());
            modelAndView.setViewName("/page/user/error");
            return modelAndView;
        }
        if (StringUtils.isEmpty(blogUser.getPassword())){
            modelAndView.addObject("errorMessage",ErrorCode.PASSWORD_NOT_BE_NULL.getMessage());
            modelAndView.setViewName("/page/user/error");
            return modelAndView;
        }
        List<BlogUser> queryUsers = userService.queryUserByUsernameAndPassword(blogUser);
        if (!CollectionUtils.isEmpty(queryUsers)){
            //登陆成功，在session中存储登录用户信息
            request.getSession().setAttribute("userInfo",queryUsers.get(0));

            List<BlogUser> friendList = userService.getFriendList(blogUser);
            //查询是否有需要处理的消息
                List<TFriendMessage> messageList = userService.getFriendMessage(queryUsers.get(0));
            if (!CollectionUtils.isEmpty(messageList)) {
                //查询名称
                List<TFriendMessage> list = userService.getUserName(messageList);
                modelAndView.addObject("flag",1);
                modelAndView.addObject("friendMessageList",list);
            }else {
                modelAndView.addObject("flag",0);
                modelAndView.addObject("friendMessageList",null);
            }

            if (!CollectionUtils.isEmpty(friendList)){
                for (BlogUser user : friendList) {
                    user.setAddTimeString(DateUtil.dateToString(user.getAddTime(),"yyyy-MM-dd HH:mm:ss"));
                }
            }
            if (CollectionUtils.isEmpty(friendList)){
                modelAndView.addObject("friendFlag",0);
            }else {
                modelAndView.addObject("friendFlag",1);
            }
            modelAndView.addObject("friendList",friendList);
            modelAndView.setViewName("page/user/friend");
            return modelAndView;
        }else {
            modelAndView.addObject("errorMessage",ErrorCode.LOGIN_FAIL.getMessage());
            modelAndView.setViewName("/page/user/error");
            return modelAndView;
        }
    }


    /**
     * 根据名称搜索用户
     * @param blogUser
     * @param request
     * @return
     */
    @PostMapping("/getUserLikeUsername")
    public ModelAndView getUserLikeUsername(BlogUser blogUser,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        BlogUser loginUser = (BlogUser) request.getSession().getAttribute("userInfo");
        modelAndView.setViewName("page/user/addfriend");
        List<BlogUser> list = userService.getUserLikeUsername(blogUser,loginUser);
        if (CollectionUtils.isEmpty(list)){
            modelAndView.addObject("flag",1);
        }
        modelAndView.addObject("userList",list);
        return modelAndView;
    }


    /**
     * 发送请求添加信息
     * @param blogUser
     * @param request
     * @return
     */
    @GetMapping("/addFriend")
    public ModelAndView addFriend(BlogUser blogUser,HttpServletRequest request){
        BlogUser loginUser = (BlogUser) request.getSession().getAttribute("userInfo");
        ModelAndView modelAndView = new ModelAndView();
        Integer i = userService.addFriendMessage(blogUser,loginUser);
        modelAndView.setViewName("page/user/addfriend");
        return modelAndView;
    }

    /**
     * 同意申请
     * @param friendMessage
     * @param request
     */
    @GetMapping("/agree")
    public ModelAndView agree(TFriendMessage friendMessage,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        BlogUser userInfo = (BlogUser) request.getSession().getAttribute("userInfo");
        boolean flag = userService.agree(friendMessage,userInfo);

        List<BlogUser> friendList = userService.getFriendList(userInfo);
        //查询是否有需要处理的消息
        List<TFriendMessage> messageList = userService.getFriendMessage(userInfo);
        if (!CollectionUtils.isEmpty(messageList)) {
            //查询名称
            List<TFriendMessage> list = userService.getUserName(messageList);
            modelAndView.addObject("flag",1);
            modelAndView.addObject("friendMessageList",list);
        }else {
            modelAndView.addObject("flag",0);
            modelAndView.addObject("friendMessageList",null);
        }

        if (!CollectionUtils.isEmpty(friendList)){
            for (BlogUser user : friendList) {
                user.setAddTimeString(DateUtil.dateToString(user.getAddTime(),"yyyy-MM-dd HH:mm:ss"));
            }
        }
        if (CollectionUtils.isEmpty(friendList)){
            modelAndView.addObject("friendFlag",0);
        }else {
            modelAndView.addObject("friendFlag",1);
        }
        modelAndView.addObject("friendList",friendList);
        modelAndView.setViewName("page/user/friend");
        return modelAndView;

    }

    /**
     * 拒绝申请
     * @param friendMessage
     * @param request
     */
    @GetMapping("/disAgree")
    public ModelAndView disAgree(TFriendMessage friendMessage,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        BlogUser userInfo = (BlogUser) request.getSession().getAttribute("userInfo");
        boolean flag = userService.disAgree(friendMessage,userInfo);

        List<BlogUser> friendList = userService.getFriendList(userInfo);
        //查询是否有需要处理的消息
        List<TFriendMessage> messageList = userService.getFriendMessage(userInfo);
        if (!CollectionUtils.isEmpty(messageList)) {
            //查询名称
            List<TFriendMessage> list = userService.getUserName(messageList);
            modelAndView.addObject("flag",1);
            modelAndView.addObject("friendMessageList",list);
        }else {
            modelAndView.addObject("flag",0);
            modelAndView.addObject("friendMessageList",null);
        }

        if (!CollectionUtils.isEmpty(friendList)){
            for (BlogUser user : friendList) {
                user.setAddTimeString(DateUtil.dateToString(user.getAddTime(),"yyyy-MM-dd HH:mm:ss"));
            }
        }
        if (CollectionUtils.isEmpty(friendList)){
            modelAndView.addObject("friendFlag",0);
        }else {
            modelAndView.addObject("friendFlag",1);
        }
        modelAndView.addObject("friendList",friendList);
        modelAndView.setViewName("page/user/friend");
        return modelAndView;
    }

    /**
     * 我的博客列表
     */
    @GetMapping("/myBlogList")
    public ModelAndView myBlogList(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        BlogUser userInfo = (BlogUser) request.getSession().getAttribute("userInfo");
        BlogVO blogVO = new BlogVO();
        blogVO.setBlogauthor(userInfo.getId().toString());
        blogVO.setUserName(userInfo.getUsername());
        List<BlogDTO> blogList = contentFeignService.blogList(blogVO);
        modelAndView.setViewName("/page/user/list");
        modelAndView.addObject("blogDTOList",blogList);
        return  modelAndView;
    }

}
